//
//  HHImageTableListCell.m
//  Smart
//
//  Created by Hayder on 2022/12/27.
//

#import "HHImageTableListCell.h"
#import "HHGlobalDefine.h"

@interface HHImageTableListCell()

@property (nonatomic, strong) UIControl *eventControl;

@end

@implementation HHImageTableListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.blankView addSubview:self.titleLabel];
        [self.blankView addSubview:self.iconImageView];
        [self.blankView addSubview:self.arrowImageView];
        [self.blankView addSubview:self.eventControl];
    }
    return self;
}

- (void)clickButtonEvent
{
    if(self.item.didClickImageCellBlock)
    {
        self.item.didClickImageCellBlock(self.item);
    }
}

- (void)setItem:(HHImageListItem *)item
{
    _item = item;
    
    [self setConfigWithItem:item];
    
    if([item.iconImage isKindOfClass:[UIImage class]])
    {
        self.iconImageView.image = item.iconImage;
    }else
    {
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:item.iconImage] placeholderImage:item.placeholderImage];
    }
    self.titleLabel.text = self.item.title;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat contentW = self.blankView.width;
    CGFloat contentH = self.blankView.height;
    
    self.titleLabel.frame = CGRectMake(self.margin, 0, self.item.titleDefaultWidth, contentH);
    self.arrowImageView.frame = CGRectMake(contentW-25-self.margin, (contentH-25)/2, 25, 25);
    self.iconImageView.frame = CGRectMake(self.arrowImageView.left - 3 - 50, (contentH - 50)/2, 50, 50);
    self.eventControl.frame = CGRectMake(self.titleLabel.right + self.margin, 0, contentW - self.titleLabel.right - 2*self.margin, contentH);
}

- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = Font(15);
        _titleLabel.textColor = kGray_51;
    }
    return _titleLabel;
}

- (UIImageView *)iconImageView
{
    if(!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] init];
        [_iconImageView cornerRadius:25];
    }
    
    return _iconImageView;
}

- (UIControl *)eventControl
{
    if(!_eventControl)
    {
        _eventControl = [[UIControl alloc] init];
        [_eventControl addTarget:self action:@selector(clickButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _eventControl;
}

- (UIImageView *)arrowImageView
{
    if(!_arrowImageView)
    {
        UIImageView *arrowImageView = [[UIImageView alloc] init];
        arrowImageView.image = BundlePngImage(@"hh_arrow");
        _arrowImageView = arrowImageView;
    }
    return _arrowImageView;
}

@end
